package com.etc.demo1.demo.controller;

import com.etc.demo1.demo.domain.User;
import com.etc.demo1.demo.mapper.UserMapper;
import com.etc.demo1.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Slf4j
@RequestMapping("user")
@RestController
public class UserController {

    private UserService userService;

    //查询全部
    @RequestMapping("/list")
    public String userList(Model model){
        List<User> users=userService.findAll();
        model.addAttribute("users",users);
        return "index";
    }

    //新增数据
    @RequestMapping("/add")
    public String save(User user){
        userService.save(user);
        return "redirect:/list";
    }

    //删除数据
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException{
        int count=userService.delete(id);
        if (count==1){
            servletResponse.sendRedirect("/list");
        }
        return "error";
    }

    //根据id查找
    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model,@PathVariable int id){
        User users=userService.getUserById(id);
        model.addAttribute("users",users);
        return "modifi";
    }

    //更新数据
    @PostMapping("/update")
    public String updateUser(Model model, User user, HttpServletRequest request){
        String id=request.getParameter("id");
        User userByid=userService.getUserById(Integer.parseInt(id));
        userService.update(user);
        System.out.println(user);
        return "redirect:/list";
    }


    @Autowired
    private UserMapper userMapper;

    @GetMapping("login")
    public String login(int name,String password){

        if(StringUtils.isEmpty(name)){
            return "用户名不允许为空";
        }
        if (StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }

        User user=userMapper.selectUser(name,password);
        if (user==null){
            return "登录失败";
        }
        if (Objects.equals(password,user.getUser_password())){
            return "登录成功";
        }

        return "登陆失败密码错误";
    }

    @GetMapping("register")
    public String register(int name,String password){
        log.info("name:{}",name);
        log.info("password:{}",password);
        if(StringUtils.isEmpty(name)){
            return "用户名不允许为空";
        }
        if (StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }

        int resultCount=userMapper.saveUser(name,password);
        if (resultCount==0){
            return "注册失败";
        }

        return "注册成功";
    }
}
